/**
 * 反馈评分课程
 * @param buy 课程购买状态
 */
function courseFeedback() {
    Zepto.popup('.feedbackPopup');
    Zepto(".feedbackPopup").on('closed',function () {
        Zepto("#comment").val("");
        playPause(2);
    });
    InitializeTheSlider();
    //暂停播放
    playPause(1);

}


function  InitializeTheSlider (){
    //滑块初始化
    Zepto('.zp-range-single-slider').zptSlider({
        from: 0,
        to: 100,
        step: 20,
        scale: ['非常差', '差', '正常', '好', '非常好'],
        format: '%s',
        unit: '%',
        width: 300,
        showLabels: true,
        showScale: true,
        theme: 'theme-blue',
        onstatechange: function (value) {
            // 数值改变时的回调
        }
    });
}

/**
 * 暂停或播放
 * @param type 1暂停，2播放
 */
function playPause(type) {
    // 反馈弹出时暂停音视频播放
    var elements = document.getElementsByTagName('audio');
    if (elements.length == 0) {
        elements = document.getElementsByTagName('video');
    }
    if (elements.length > 0) {
        var element = elements[0];
        if(type==1){
            element.pause();
            // 隐藏是因为万恶的QQ浏览器/UC浏览器的视频浮层
            Zepto(element).hide();
        }else if(type==2){
            element.play();
            // 隐藏是因为万恶的QQ浏览器/UC浏览器的视频浮层
            Zepto(element).show();
        }
    }
}

/**
 * 加载评论数据列表
 * @param pageNo 页码
 */
function ajaxCommentList(pageNo) {
    var params = {
        'courseId':courseId
    };
    params = JSON.stringify(params);
    loadMobilePathData({
        url: baseloaction + '/mobile/course/common/ajaxCourseCommentList',
        contextId: 'course-comment-list',
        pageNo: pageNo,
        pageCall: ajaxCommentList,
        params: params,
        callback: function (pageNo) {
            if(count == null || count == 'undefined' || count == ''){
                count = 0;
            }
            $("#course-comment-count").text('全部评价('+count+')');
        }
    });
}

/**
 * 评价前验证
 * @param courseId 课程ID
 * @param catalogId 目录ID
 * @param callback 成功后回调函数
 */
function checkComment(courseId,catalogId,callback) {
    var params = {
        'courseId': courseId,
        'catalogId': catalogId
    };
    params = JSON.stringify(params);
    Zepto.ajax({
        url: baseloaction + '/mobile/uc/course/common/checkCourseCommentBefore',
        data: params,
        dataType: 'json',
        type: 'post',
        contentType: 'application/json;charset=utf-8',
        success: function (result) {
            if(result.success){
                if($.isFunction(callback)){
                    callback();
                }
            }else {
                Zepto.toast(result.message);
            }
        },
        error:function () {
            Zepto.toast('响应不成功');
        }
    });
}

/**
 * 保存反馈
 * @param quality 课程质量(1~5)
 * @param teach 老师讲法(1~5)
 * @param feel 综合感受(1~5)
 * @param comment 评论内容
 * @param courseId 课程ID/套餐ID
 * @param catalogId 评价的课程章节
 * @param callback 保存成功后回调
 */
function saveComment(quality,teach,feel,comment,courseId,catalogId,callback) {
    quality = value(quality);
    teach = value(teach);
    feel = value(feel);
    var params = {
        'courseId': courseId,
        'catalogId': catalogId,
        'comment': comment,
        'quality': quality,
        'teach': teach,
        'feel': feel
    };
    params = JSON.stringify(params);
    Zepto.ajax({
        url: baseloaction + '/mobile/uc/course/common/saveCourseComment',
        data: params,
        dataType: 'json',
        type:'post',
        contentType:'application/json;charset=utf-8',
        success: function (response) {
            if (response.success) {
                Zepto.toast(response.message);
                if($.isFunction(callback)){
                    callback();
                }
                //重新开启播放
                playPause(2);
            } else {
                Zepto.toast(response.message);
            }
        }
    });
    // 反馈百分比转成 (0, 5]的数值
    function value(v) {
        if (v > 80) {
            return 5;
        } else if (v > 60) {
            return 4;
        } else if (v > 40) {
            return 3;
        } else if (v > 20) {
            return 2;
        } else {
            return 1;
        }
    }
}

//随堂资料下载
function downloadDatum (courseId,catalogId,that){
    isLogin(function () {
        Zepto.ajax({
            url: baseloaction + '/mobile/course/ajax/check/buy',
            data: {'courseId': courseId},
            dataType: 'json',
            success: function (response) {
                if (response.success) {
                    if(wxBrowser==false){
                            $.confirm('确认要下载本节学习资料吗</br><span style="font-size:10px;">部分浏览器不支持下载，如果遇到，</br>请更换其它浏览器<span>', function () {
                                location.href=that.data("url");
                            });
                        $('.modal-in').find('.modal-button').text('我在想想 ').css('color', '#bbb');
                        $('.modal-in').find('.modal-button-bold').text('确认下载').attr('style', '');
                    }else{
                        var prompt = $("#prompt2");
                        if(prompt==null||prompt==undefined||prompt.length==0){
                            var html =
                                '<section id="prompt2" style="display: none;">' +
                                '    <div class="prompt_bg"></div>' +
                                '    <div class="shareArrow"><em class="arrow-right-top"></em></div>' +
                                '    <div class="sharePrompt">' +
                                '        <p class="text-center">微信环境暂不支持下载，点击右上角，</br>选择用浏览器打开</p>' +
                                '    </div>' +
                                '</section>'
                            $('body').append(html);
                        }

                        $('#prompt2').css("display","block").click(function () {
                            $(this).css("display","none");
                            $('body').css('overflow', 'inherit')
                        });
                    }


                }else{
                    guideBuyCourse("你还没有购买课程，无法下载资料，确认购买吗",courseId);
                }
            },
            error: function () {
                console.log('error');
            }
        });
    });
}


function guideBuyCourse(message,courseId) {


        Zepto.ajax({
            url:baseloaction + "/mobile/course/guideBuyCourse",
            type:'post',
            data:{courseId:courseId },
            dataType:"json",
            success:function(result){
                if(result.success){
                    openConfirm(message,result.entity,courseId)
                }else{
                    Zepto.toast(0,'','系统错误。跳转失败');
                }
            },error: function(){
                Zepto.toast(0,'','系统错误。跳转失败');
            }

        });


}


function openConfirm(message,type,courseId){

    Zepto.confirm(message, function () {

        switch (type) {
            case 1:
                genFreeOrder("FREE", courseId  + '-COURSE-1', function () {
                    Zepto('.free-bm-box').remove();
                    Zepto('#c-audition-btn').remove();
                    Zepto.toast('免费报名成功');
                });
                break;
            case 2:
                window.location.href = baseloaction +"/mobile/uc/order/initPayOrder?orderType=COMMON&shopData="+courseId +"-COURSE-1";
                break;
            case 3:
                Zepto.toast(result.message);
                setTimeout(function(){
                    window.location.href = baseloaction +'/mobile/uc/member/queryAllMemberList';
                },1500);
                break;
        }

    });
    Zepto('.modal-in').find('.modal-button').text('我再想想').css('color', '#3d4145');
    Zepto('.modal-in').find('.modal-button-bold').text(type==1?'免费报名':'立即报名').attr('style', '');
}

function validationStatus() {
    if(Zepto("#abandoned").length>0){
        var  buyStatus = Zepto("#abandoned").data("buytype");
        var text  ="此课程已下架，请查看其它课程";
        if(!buyStatus){
            Zepto.alert(text,null,function () {
                window.history.go(-1);
            })
            Zepto('.modal-in').find('.modal-button-bold').text('知道了');
        };

    }
}